package com.hn658.groupshare.common.log.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Component;

import com.hn658.framework.common.AbstractBusinessObject;
import com.hn658.framework.dataaccess.pagination.GenericPage;
import com.hn658.framework.shared.context.RequestContext;
import com.hn658.framework.shared.entity.IUser;
import com.hn658.groupshare.common.enums.ModuleType;
import com.hn658.groupshare.common.log.dao.ILogMongoDBDAO;
import com.hn658.groupshare.common.log.entity.LogInfo;
import com.hn658.groupshare.common.log.service.ILogService;

/**
 * 日志管理实现
 * @author ztjie
 *
 */
@Component
public class LogServiceImpl extends AbstractBusinessObject implements ILogService{
	
	@Autowired
	ILogMongoDBDAO logMongoDBDAO;

	@Override
	public LogInfo add(ModuleType module, String operateInfo, IUser user) {
		LogInfo log = new LogInfo();
		log.setId(new Date().getTime());
		log.setCurrentThreadId(RequestContext.getCurrentContext().getRequestId());
		log.setRequestIp(RequestContext.getCurrentContext().getIp());
		log.setRequestUrl(RequestContext.getCurrentContext().getRequestURL());
		log.setCreateTime(new Date().getTime());
		log.setModule(module.getName());
		if(user!=null){
			log.setCurrentUserAccount(user.getLoginAccount());
			log.setCurrentUserId(user.getId());
		}
		log.setOperateInfo(operateInfo);
		logMongoDBDAO.insert(log);
		return log;
	}

	@Override
	public GenericPage<LogInfo> queryLog(Map<String, Object> queryMap,
			int limit, int start) {
		Query query = logMongoDBDAO.getQuery(queryMap);
		List<LogInfo> logList = (List<LogInfo>) logMongoDBDAO.findByQueryMap(query, limit, start);
		long totalSize = logMongoDBDAO.getCount(query);
		GenericPage<LogInfo> gener = new GenericPage<LogInfo>(start, totalSize, limit, logList);
		return gener;
	}
}
